The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
 

 File Ownership and Permissions

1.0 File Owners and Permissions
       Ownership
       Permissions
       Directory Permissions
       Special Permissions
           Sticky Bit
           SGID (Set Group ID bit)
           SUID (Set User ID Bit)
       chmod : Change File Permissions
           Numeric Mode
           Symbolic Mode
       Link File Permissions
       umask: Change Default File Permissions
1.1 File Attributes
       chattr : Change File Attributes
       lsattr : Display File Attributes
ENG⇒中ENG⇒中
  1.0 File Ownership and Permissions
It is said in the file-related instructions : "The file extension under Linux is only for "reference"; for example, the executable file of Linux will not use ".exe" as the file extension like Windows." So how does Linux determine which files are executable files? Why can’t some directories be entered when I log in with a normal user? Why can’t other files be deleted except for the files I created? Even many files are not allowed to be read by me? Everything is the same as The "ownership" of the file is related to the "permissions". Let's experience it
before explaining it.

Example: (Log in with a regular user account for testing, do not log in with the "root" account, because Superuser is not subject to authority regulations)
$ cd /root ←Go to the directory "/root" and play
bash: cd: /root: Permission denied ←Permission denied
$ cat /etc/shadow ← Read the account password information file "/etc/shadow"
cat: /etc/shadow: Permission denied ← Permission denied
$ cp /etc/shadow ~/ ←Copy "/etc/shadow" to see
cp: cannot open '/etc/shadow' for reading: Permission denied ←Permission denied
$ rm -f /bin/ls ←delete command "/bin/ls"
rm: cannot remove `/bin/ls': Permission denied ←Permission denied

Ownership
Because Linux is a multi-tasking & multi-user OS, that is, it can be used by many people, so there must be some mechanism to properly isolate the reading and writing of files by different logins. Write, execute and delete, or even enter a certain directory, this security mechanism is the "ownership" and "permission" of the file. The owner category of Linux files can be divided into " owner" (user), "group" and "others" as follows: There are actually two identities when logging in in Linux, one is the user and the other is the group accompanying the registrant, and if a file is created, this file will record these two identities.

example:
$ echo "hello Wold" > /tmp/myfile ←Create a file "myfile"
$ ls -l /tmp/myfile ←Use ls -l to list lengthy file information
-rw-rw-r-- 1 aaa aaa 11 2011-10-10 10:10 myfile ←The one marked in red is user, and the one marked in green is group

^ back on top ^


Permissions
File permissions determine whether the owner (user/group/others) of a file has the ability to "read," "write," or "execute" it. In Linux, the execution of a file is not determined solely by its file extension. Even executable files need to have the proper "execute" permission granted in order to be executed.

However, these file permissions, except for the "execute" permission, are only enforced for the "root" user. When logged in as the "root" user, there are no restrictions. The "root" user has unrestricted access and can perform any action, such as deleting files or modifying their contents, without any limitations. It's as if the "root" user has complete control and can do anything within the system.

Except whether these permissions of the file can be "executed". Recall the example of using ls -l to list lengthy file information when introducing the ls command, as follows:

$  ls -l ← list lengthy file contents
drw-rw-r-- aaa  aaa  292 2011-09-07  11:44  myfile
 ↑    ↑  ↑         
 permission   user  group        

In the above example, the "permissions" column lists 9 characters in total. For easy identification, it is deliberately displayed in different colors for illustration. The 9 characters of the permission are similar to "rwxrwxrwx" , representing three different ownership The meaning is as follows: The English characters "r", "w", and "x" (the order of "rwx" is fixed) appearing in the permission column of the file represent the meanings of the file as read / write /execute permission, and the permission that is not set is displayed with "-". For example, "r--" only has read permission.

The following table shows the role of permissions on files:
Permissions The role of the file
r (read) You can view the content of the file, such as cat or less or other tools to read.
w (write) Its content can be changed, such as vi or >> (STDOUT Append Redirection) can be used to change the content of the file.
x (execute) If it is an executable file, it can be executed, but if it is a shell script, it must have read permission before it can be executed (because the shell needs to read the file)

example:
$ ls -l /tmp/myfile
-rw-rw-r-- 1 aaa aaa 11 2011-10-10 10:10 myfile

例:
In the above example , the output permission of ls -l is displayed as "rw-rw-r--", the first character of the red character is "r", which means that the owner (this is the account "aaa") has "read" permission for this file , the second character is "w", which means the owner has "write" permission for this file, and the third character "-", does not have "w", but shows "-", which means the owner has no permission for this file. permission to execute.

The same green font means that the group "aaa" also has read and write permissions for this file but has no execution permission, while other has no specification for both the owner and the group owner, and can only read and cannot write. Next, let's test it with different accounts.

example:
$ echo "hello Wold" > /tmp/myfile ←Create a file "myfile"
$ ls -l /tmp/myfile ←Verify permissions
-rw-rw-r-- 1 aaa aaa 15 2011-09-07 00:46 /tmp/myfile ← the same owner/group can read and write but others can only read
$ cat /tmp/myfile← read and see (test "read" permission)
Hello Wold
$ echo 'Hello Linux' >> /tmp/myfile ← change take a look at the content of "myfile" (test the "write" permission)
$ cat /tmp/myfile ← Verify and see
Hello World
Hello Linux
$ su bbb ← Temporarily change other account
Password: ← Enter its account password
$ cat myfile ← Test "Read" permission
Hello World ←This file other has "read" permission, so other people can read the file created by the account aaa
Hello Linux
$ echo 'Hello Unix' >> /tmp/myfile←Add the content of "myfile" to see
bash: /tmp/myfile: Permission denied ←Permission denied (because "others" do not have the "write" permission for this file)

Let's look at other examples.

Ex:
$ ls -l /etc/shadow ←The long file format of the information file "/etc/shadow" that lists the account password
-r-------- 1 root root 1147 2011-09-07 11:47 /etc/shadow

The file "/etc/shadow" in the above example is the shadow file of the account password. Although the contents are all ASCII-encoded text files, they have been encrypted . Because this file is very important, it may be cracked by a hacker to know the login password of each account, so the owner and group of this file are "root", and its permissions are displayed as "r--------" Indicates that this file can only be read and cannot be written even if you log in with "root", but not being able to write to "root" is a declaration that is greater than the reality, because the account "root" has unlimited privileges.

Let's take a look at what the owner and permissions of a general executable file look like.

Ex:
$ ls -l /bin/cat
-rwxr-xr-x 1 root root 23360 2007-10-31 11:52 /bin/cat

In the above example, the owner and group of the cat command is "root" and can be fully read, written, and executed, while the owner and group other than "root" who use this command are counted as others and can only read and execute.

In addition, some commands such as cp will change the owner and group of the target file, making it owned by the operator. The purpose is to hope that the copied file can be fully controlled.

Example: (Please log in with a regular user account to test)
$ cp /bin/cat ~/ ←copy "/bin/ls" to home directory
$ ls -l /bin/cat  ~/cat ←list these 2 files for comparison
-rwxr-xr-x 1 root root 23360 2007-10-31 11:52 /bin/cat
-rwxr-xr-x 1 aaa  aaa  23360 2011-10-10 10:10 ./cat ←Did you notice that the copied file, the owner and the group have changed to the operator all

Then how to keep the original taste of the copied files? For example, when backing up the system files, you can't even change the original owner and permissions. If you want to restore the system, the world will be in chaos. Use the cp -a or cp -p option to keep the owner and permissions of the original file.

^ back on top ^


Permissions of Directories
The permissions of directories are somewhat different from those of files, as shown in the following table:
permissions The effect on the directory
r List the files in the directory or its subdirectories (such as using ls to display the files in the directory but not using cat to read the files in the directory)
w Add, delete or rename files in the directory (for example, you can operate rm , cp , or mv on the files in the directory, etc.)
x Enter the directory or execute the program in the directory (for example, cd can enter the directory)

Of course, these permissions in the directory are not binding on"'root".

example:
$ ls -l /home ←List all home directory permissions in "/home"
drwx------ 28 aaa aaa 4096 4096 2-11-10-11 15:41 aaa
drwx------ 3  bbb bbb 4096 4096 2-11-10-03 19:59 bbb
$ ls /home/bbb ← See what is in other people’s homes?
ls: cannot open directory /home/bbb: Permission denied ← No permission

The above example shows that the owner of each person’s home directory has the “x” permission, so they can freely enter their own home directory, and has the “rw” permission, so they can list and add or delete files in their own home directory, but the use of other accounts Those who are other will be blocked at the door of the house (except account "root").

Special Permissions
Although regular file permissions can meet most requirements, there are gray areas. For example, consider a file with permissions set as "rw- --- ---." In theory, only the owner should be able to modify the file's contents. However, the "others" category has permissions set as "--- --- -wx" for a directory. This means that "others" have "x" (permission to enter the directory) and "w" (permission to add or remove files within the directory). Consequently, it appears that others can enter the directory and delete or rename files that do not belong to the owner.

When file and directory permissions contradict each other, it is important to determine which permissions take precedence. In such cases, the system typically follows a set of predefined rules or priorities to resolve the conflicts.

To address vulnerabilities and potential security risks, special permissions play a crucial role. They utilize the position of the "x" (execute) permission to set and display additional permissions. If you see a "t" or "s" displayed in the place of the "x" permission, it indicates the presence of special permissions.

Special permissions serve as a mechanism to address and prevent vulnerabilities, ensuring that the appropriate access controls are in place for files and directories.

The following is an overview of the purpose and usage of special permissions:
special permission file/directory effect proviso ls -l show
Sticky bit directory In a directory with a sticky bit, only the owner of the file or "root" can delete or rename the file oher also have "x" permission --- --- --t
Set Group ID file(s) Only for executable files, the group to which the file belongs will be temporarily changed during execution group  also has "x" permission --- --s ---
directory Any files placed in the directory will become the group of this directory
Set User ID file(s) Only for the execution file, the owner of the file will be temporarily changed during execution user must also have "x" permission --s --- ---

^ back on top ^


chmod : Change File Permissions
The command chmod  (change mode) can change the permissions and special permissions of files and directories. Only the owner of the file and "root" have the right to change its permissions. chmod accepts "Numeric mode" and "Symbolic mode".


^ back on top ^


Link File Permissions
f it is a linked file , should the permissions be based on the original file or its clone? If it is a symbolic linked file, if you use ls -l to query its permissions, it will always display "lrwxrwxrwx". But its meaning is not that anyone can read/write/execute (enter the directory), but "completely according to the permission of the original file". For example, changing the permission of the symbolic link file is actually changing the permission of the original file, and the symbolic link file is allways displayed "lrwxrwxrwx".

If it is a hard link , because the hard link is same file, so changing the it will change together.

umask: Change The Default File Permissions
If you log in with a regular user account to create a file, most Linux distributions have the default file permissions as "rw-rw-r--" (other has no write permissions); if you create a file for Superuser The file limit of the file will be a little more, and its file permission is "rw- r-- r--" (group and other have no write permission). The command umask can change the default permissions for creating files or directories.

Example: (This is a regular user account test, if you use "root" test, the result will be different)
$ umask ←If there is no option, the output is to show the default permission
0002

The numbers output by umask are compared with the following table for the permissions to create files or directories.
umask Default File Permissions Default Directory Permissions
000
666 (rw- rw- rw-)
777 (rwx rwx rwx)
002
664 (rw- rw- r--)
775 (rwx rwx r-x)
022
644 (rw- r-- r--)
755 (rwx r-x r-x)
027
640 (rw- r-- ---)
750 (rwx r-x ---)
077
600 (rw- --- ---)
700 (rwx --- ---)
277
400 (r-- --- ---)
500 (r-x --- ---)

It can be seen from the table that umask= 0002 means that the permission to create a file is "rw-rw-r--", and if it is a directory, the permission is "rwx rwx rx".

If you want to change the default permissions for creating files, just enter umask [#][###] ("#" is a digit, refer to the above table, and the first "#" is a special permission, which is generally not changed, so keep 0) .

Example: (This is a regular user account test, if you use "root" test, the result will be different)
$ umask ← Look at the default permission
0002
$ echo 'umask file1' > umask_0002 ← Create a file "umask_0002"
$ umask 0027 ← Change the default permission to "0027" (rw- r-- ---)
$ umask ← Confirm See if the default permission has been changed to
0027 ← changed to "0027"
$ echo 'umask file2' > umask_0027 ←Use umask=0027 to create a file "umask_0027"
$ ls -l umask_0002 umask_0027 ← List the permissions of the two files for comparison
-rw-rw-r-- 1 aaa  aaa  12 2011-10-10 10:10 umask_0002 ← Note that the permissions of the two files are different
-rw-r----- 1 aaa  aaa  12 2011-10-10 10:11 umask_0027

General application lookup table should be enough, if you want to ask 「Why umask = 0002, the permission to create a new file is "rw-rw-r--"」? You can continue to read.

Umask is the abbreviation of "user mask", and "mask" in computer engineering. The meaning of mask is "to change a bit by bitwise operation". The Boolean logic operation formula ( Boolean operators) is " Inital value & ~ umask value " (initial value file is 666, directory is 777) or the following logic circuit operation.



linux umask

Because it is not necessary to execute the text files, the initial value of the permission "666" (rw-rw-rw-).
In the previous sample test, put umask=002, inital value=666 into the formula "666 & ~002", and the result is "664" (rw-r--r--).
In another example, the initial value of umask=027 is 666, which is brought into the expression "666 & ~027" = "640" (rw- r-- ---).

If a directory is created, this directory can be entered due to requirements, so the initial value is "777" (rwx rwx rwx).
Take umask=002 as an example to import the formula "777 & ~002" = "775" (rwx rwx rx).

^ back on top ^


  1.1 File attributes

File permissions are not binding on the account "root", Therefore, if you accidentally make a mistake and issue the command rm -fr / (the root directory and all subdirectories and files will be killed without mercy), then Linux will probably die. Is there any way to regulate root? That is "file attributes", can make up for the danger of too much root power.

chattr : Change File Attributes
chatttr ( change attribute ) can change 16 attributes of Linux files, only about 8~14 are defined (the number of attributes and definitions supported by different releases may be different) . The usage is somewhat similar to chmod , with "+" to add attributes; "-" to subtract attributes; "=" to directly set attributes, the syntax is as follows:
Syntax: chattr [-otpiton][+-=][mode] file/directroy
Command name/Function/Command user Options Function The attributes of mode are [aAcdDisSu], each meaning is
chattr/
change attribute/
Any(mainly for Superuser)
-R Change the attributes of files recursively
"a": used for files can only be accumulated, can not be deleted. When used in a directory, only the content of the files in the directory can be modified, but the files cannot be added or deleted. And only "root" can use .
"A": Do not update the atime of the file
"c": automatically compress files when archiving, and automatically decompress when reading
"d": Exclude dump data, that is, this file will be excluded when executing the dump command
"D" If the directory is set, the data in the buffer memory will be saved to the hard disk synchronously
"i": Make the file indestructible; including cannot delete, rename, modify content, change permissions, etc. and only "root" can use it
"s": Destroy corpses and wipe out traces. When a file is deleted, the block of the file will be filled with 0 (so that those who are interested cannot restore the file)
"S": When archiving, do not write to the buffer memory first, but directly write to the hard disk (the performance is poor, but it can prevent file loss when the power is cut off)
"u": Contrary to "s" destroying and erasing traces, when the file is just deleted, only the inode is deleted, so that the file can be saved if you regret it in the future "maybe"
-V The process of displaying transaction attributes

From chattr 's mode usage, we can know that many attributes are for standardizing "root" (Supperuser). For example, the attribute "a" is mainly used for the record file that will accumulate. Even with the "root" account, this file cannot be deleted. The attribute "i" can't even touch this one.

When deleting a file, Linux only deletes the inode of the file and does not actually clear the data (block) for efficiency. If there is confidential data, it is right to add the attribute "s" for fear of being restored when deleting.

Experiment: (log in as "root", test)
# echo '123' > myfile ←Create a file "myfile"
# chattr +a myfile ←Set "a" attribute can only be accumulated, but cannot be deleted
# rm -f myfile ←Delete and see
rm: cannot remove `myfile': Operation not permitted ←denied
# echo '456' > myfile ← overwrite to see
bash: myfile: Operation not permitted ←denied
# echo '456' >> myfile ← accumulate to see
cat myfile ← verify
123
456
# chattr -V -a myfile ← Subtract the "a" attribute to be deleted
chattr 1.40.2 (12-Jul-2007)
Flags of myfile set as ----------------

Experiment: (log in as "root", test)
# chattr =aAdS file ←Set the file to have attributes "a", "A", "d", "S"
# chattr -V +i -ad file ←Add the attribute "i" to the file and subtract "a", " d" attribute

lsattr : Displays File Attributes.
Sometimes there may be no problem with permissions , but some files cannot be deleted or modified. At this time, you can use lsattr to check whether the attributes have been changed by chattr . lsattr is the abbreviation of l i s t attr bute, use To check the properties of the file, the usage is as follows:


Syntax::lsattr [-otpiton] file/directroy
command name/function/command user

options Function
lsattr/
list attrbute/
Any
-a List all files and directories, including hidden files and properties of the working directory and the upper directory
-d List only directory properties
-R Recursively list files and subdirectories under a directory attribute

Example: (logged in as root)
# echo > file
# chattr =aAdS file ←Set the file to have "a", "A", "d", "S" attributes
# lsattr file ←Display file attributes
--S--adA-------- file

Example:
# lsattr -a ←List the attributes of all files in the directory (including the working directory and its parent directory)
-----a---------- ./. ←Attributes of the working directory
---------------- ./.. ← Attributes of the upper directory
-----a---------- ./jun ← Attributes of files in the directory


^ back on top ^